package com.adacms.test;

import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;

import com.openyelp.data.apps.ObjectFactory;
import com.openyelp.data.entity.Area;
import com.openyelp.data.entity.Shop;
import com.openyelp.data.entity.ShopCategory;
import com.openyelp.data.service.AreaService;
import com.openyelp.data.service.ShopCategoryService;
import com.openyelp.data.service.ShopService;
import com.openyelp.lbs.webservice.domain.BackDetailInfo;
import com.openyelp.lbs.webservice.domain.BackPoi;
import com.openyelp.lbs.webservice.domain.Backs;
import com.openyelp.lbs.webservice.v2.PlaceService;

public class BaiduLbsShopRpc2 {

	static String name="家居服务";
	static int catalog=92;

	public static void main(String[] args) {

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				AreaService areaservice = RpcUtils.get(AreaService.class);
				pp(areaservice.findById(111), name);
			}
		}).start();
		;
	}

	public static void pp(Area area2, String name) {
		ShopService service = RpcUtils.get(ShopService.class);
		try {
			System.out.println("当前地区" + area2.getName());
			PlaceService s = new PlaceService();
			s.setRegion(area2.getName());
			int max = 0;
			int index = 1;
			do {
				Backs b = s.searchByKey(name, 30, index);

				max = add(service, b, area2);
				System.out.println("max:" + max + "   index:" + index);

				index++;
			} while (max > 0);

		} catch (Exception e) {
		}
	}

	private static int add(ShopService service, Backs b, Area area) {
		int result = 0;
		if (b != null) {
			List<BackPoi> pois = b.getResults();
			result = pois.size();
			for (BackPoi backPoi : pois) {
				Shop shop = new Shop();
				shop.setAddDate(new Date());
				shop.setAddress(backPoi.getAddress());
				shop.setUid(backPoi.getUid());
				if (backPoi.getDetail_info() != null) {
					shop.setWebsite(backPoi.getDetail_info().getDetail_url());
				}
				if (backPoi.getLocation() != null) {
					shop.setGps(backPoi.getLocation().getgps());
					shop.setLatitude((double) backPoi.getLocation().getLat());
					shop.setLongitude((double) backPoi.getLocation().getLng());

					shop.setArea(area);
					shop.setPhone(backPoi.getTelephone());
					shop.setName(backPoi.getName());

					BackDetailInfo detail = backPoi.getDetail_info();
					if (detail != null) {

						ShopCategory c = new ShopCategory();
						c.setId(catalog);
						shop.getCategorys().add(c);
						;
						shop.setWebsite(backPoi.getDetail_info()
								.getDetail_url());
						String tags = backPoi.getDetail_info().getTag();
						shop.setDetail_url(detail.getDetail_url());
						shop.setDistance(detail.getDistance());
						shop.setCheckin_num(stringToInteger(detail
								.getCheckin_num()));
						// price string poi商户的价格
						shop.setPrice(stringToFloat(detail.getPrice()));
						// shop_hours string 营业时间
						shop.setShop_hours(detail.getShop_hours());
						// overall_rating string 总体评分
						shop.setOverall_rating(stringToFloat(detail
								.getOverall_rating()));
						// taste_rating string 口味评分
						shop.setTaste_rating(stringToFloat(detail
								.getTaste_rating()));
						// service_rating string 服务评分
						shop.setService_rating(stringToFloat(detail
								.getService_rating()));
						// environment_rating string 环境评分
						shop.setEnvironment_rating(stringToFloat(detail
								.getEnvironment_rating()));
						// facility_rating string 星级（设备）评分
						shop.setFacility_rating(stringToFloat(detail
								.getFacility_rating()));
						// hygiene_rating string 卫生评分
						shop.setHygiene_rating(stringToFloat(detail
								.getHygiene_rating()));
						// technology_rating string 技术评分
						shop.setTechnology_rating(stringToFloat(detail
								.getTechnology_rating()));
						// image_num string 图片数
						shop.setImage_num(stringToFloat(detail.getImage_num()));
						// groupon_num int 团购数
						shop.setGroupon_num(detail.getGroupon_num());
						// discount_num int 优惠数
						shop.setDiscount_num(detail.getDistance());
						// comment_num string 评论数
						shop.setComment_num(stringToInteger(detail
								.getComment_num()));
						// favorite_num string 收藏数
						shop.setFavorite_num(stringToInteger(detail
								.getFavorite_num()));
						// checkin_num string 签到数
						shop.setCheckin_num(stringToInteger(detail
								.getCheckin_num()));
						service.save(shop);
						if (tags != null) {
							String[] ts = tags.split(";");
							for (String string : ts) {
								service.addTag(shop, string);
							}
						}
					} else {
						Shop s = service.save(shop);
					}
				}

			}
		}
		return result;
	}

	public static Float stringToFloat(String floats) {
		Float result = -1f;
		try {
			result = Float.valueOf(floats);
		} catch (Exception e) {
		}
		return result;
	}

	public static Integer stringToInteger(String floats) {
		Integer result = -1;
		try {
			result = Integer.valueOf(floats);
		} catch (Exception e) {
		}
		return result;
	}
}
